home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.4 KB  |  279 lines

  1. /* Copyright (C) 1992, 1993, 1994, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsutil.c,v 1.4 2000/09/19 19:00:33 lpd Exp $ */
  20. /* Utilities for Ghostscript library */
  21. #include "string_.h"
  22. #include "memory_.h"
  23. #include "gstypes.h"
  24. #include "gconfigv.h"        /* for USE_ASM */
  25. #include "gsmemory.h"        /* for init procedure */
  26. #include "gsrect.h"        /* for prototypes */
  27. #include "gsuid.h"
  28. #include "gsutil.h"        /* for prototypes */
  29.  
  30. /* ------ Unique IDs ------ */
  31.  
  32. /* Generate a block of unique IDs. */
  33. static ulong gs_next_id;
  34.  
  35. init_proc(gs_gsutil_init);    /* check prototype */
  36. int
  37. gs_gsutil_init(gs_memory_t *mem)
  38. {
  39.     gs_next_id = 1;
  40.     return 0;
  41. }
  42.  
  43. ulong
  44. gs_next_ids(uint count)
  45. {
  46.     ulong id = gs_next_id;
  47.  
  48.     gs_next_id += count;
  49.     return id;
  50. }
  51.  
  52. /* ------ Memory utilities ------ */
  53.  
  54. /* Transpose an 8 x 8 block of bits.  line_size is the raster of */
  55. /* the input data.  dist is the distance between output bytes. */
  56. /* This routine may be supplanted by assembly code. */
  57. #if !USE_ASM
  58.  
  59. void
  60. memflip8x8(const byte * inp, int line_size, byte * outp, int dist)
  61. {
  62.     uint aceg, bdfh;
  63.  
  64.     {
  65.     const byte *ptr4 = inp + (line_size << 2);
  66.     const int ls2 = line_size << 1;
  67.  
  68.     aceg = ((uint)*inp) | ((uint)inp[ls2] << 8) |
  69.         ((uint)*ptr4 << 16) | ((uint)ptr4[ls2] << 24);
  70.     inp += line_size, ptr4 += line_size;
  71.     bdfh = ((uint)*inp) | ((uint)inp[ls2] << 8) |
  72.         ((uint)*ptr4 << 16) | ((uint)ptr4[ls2] << 24);
  73.     }
  74.  
  75.     /* Check for all 8 bytes being the same. */
  76.     /* This is especially worth doing for the case where all are zero. */
  77.     if (aceg == bdfh && (aceg >> 8) == (aceg & 0xffffff)) {
  78.     if (aceg == 0)
  79.         goto store;
  80.     *outp = -((aceg >> 7) & 1);
  81.     outp[dist] = -((aceg >> 6) & 1);
  82.     outp += dist << 1;
  83.     *outp = -((aceg >> 5) & 1);
  84.     outp[dist] = -((aceg >> 4) & 1);
  85.     outp += dist << 1;
  86.     *outp = -((aceg >> 3) & 1);
  87.     outp[dist] = -((aceg >> 2) & 1);
  88.     outp += dist << 1;
  89.     *outp = -((aceg >> 1) & 1);
  90.     outp[dist] = -(aceg & 1);
  91.     return;
  92.     } {
  93.     register uint temp;
  94.  
  95. /* Transpose a block of bits between registers. */
  96. #define TRANSPOSE(r,s,mask,shift)\
  97.   (r ^= (temp = ((s >> shift) ^ r) & mask),\
  98.    s ^= temp << shift)
  99.  
  100. /* Transpose blocks of 4 x 4 */
  101.     TRANSPOSE(aceg, aceg, 0x00000f0f, 20);
  102.     TRANSPOSE(bdfh, bdfh, 0x00000f0f, 20);
  103.  
  104. /* Transpose blocks of 2 x 2 */
  105.     TRANSPOSE(aceg, aceg, 0x00330033, 10);
  106.     TRANSPOSE(bdfh, bdfh, 0x00330033, 10);
  107.  
  108. /* Transpose blocks of 1 x 1 */
  109.     TRANSPOSE(aceg, bdfh, 0x55555555, 1);
  110.  
  111. #undef TRANSPOSE
  112.     }
  113.  
  114.   store:
  115.     *outp = (byte)aceg;
  116.     outp[dist] = (byte)bdfh;
  117.     outp += dist << 1;
  118.     *outp = (byte)(aceg >>= 8);
  119.     outp[dist] = (byte)(bdfh >>= 8);
  120.     outp += dist << 1;
  121.     *outp = (byte)(aceg >>= 8);
  122.     outp[dist] = (byte)(bdfh >>= 8);
  123.     outp += dist << 1;
  124.     *outp = (byte)(aceg >> 8);
  125.     outp[dist] = (byte)(bdfh >> 8);
  126. }
  127.  
  128. #endif /* !USE_ASM */
  129.  
  130. /* Get an unsigned, big-endian 32-bit value. */
  131. ulong
  132. get_u32_msb(const byte *p)
  133. {
  134.     return ((uint)p[0] << 24) + ((uint)p[1] << 16) + ((uint)p[2] << 8) + p[3];
  135. }
  136.  
  137. /* ------ String utilities ------ */
  138.  
  139. /* Compare two strings, returning -1 if the first is less, */
  140. /* 0 if they are equal, and 1 if first is greater. */
  141. /* We can't use memcmp, because we always use unsigned characters. */
  142. int
  143. bytes_compare(const byte * s1, uint len1, const byte * s2, uint len2)
  144. {
  145.     register uint len = len1;
  146.  
  147.     if (len2 < len)
  148.     len = len2;
  149.     {
  150.     register const byte *p1 = s1;
  151.     register const byte *p2 = s2;
  152.  
  153.     while (len--)
  154.         if (*p1++ != *p2++)
  155.         return (p1[-1] < p2[-1] ? -1 : 1);
  156.     }
  157.     /* Now check for differing lengths */
  158.     return (len1 == len2 ? 0 : len1 < len2 ? -1 : 1);
  159. }
  160.  
  161. /* Test whether a string matches a pattern with wildcards. */
  162. /* '*' = any substring, '?' = any character, '\' quotes next character. */
  163. const string_match_params string_match_params_default = {
  164.     '*', '?', '\\', false
  165. };
  166.  
  167. bool
  168. string_match(const byte * str, uint len, const byte * pstr, uint plen,
  169.          register const string_match_params * psmp)
  170. {
  171.     const byte *pback = 0;
  172.     const byte *spback = 0;    /* initialized only to pacify gcc */
  173.     const byte *p = pstr, *pend = pstr + plen;
  174.     const byte *sp = str, *spend = str + len;
  175.  
  176.     if (psmp == 0)
  177.     psmp = &string_match_params_default;
  178.   again:while (p < pend) {
  179.     register byte ch = *p;
  180.  
  181.     if (ch == psmp->any_substring) {
  182.         pback = ++p, spback = sp;
  183.         continue;
  184.     } else if (ch == psmp->any_char) {
  185.         if (sp == spend)
  186.         return false;    /* str too short */
  187.         p++, sp++;
  188.         continue;
  189.     } else if (ch == psmp->quote_next) {
  190.         if (++p == pend)
  191.         return true;    /* bad pattern */
  192.         ch = *p;
  193.     }
  194.     if (sp == spend)
  195.         return false;    /* str too short */
  196.     if (*sp == ch ||
  197.         (psmp->ignore_case && (*sp ^ ch) == 0x20 &&
  198.          (ch &= ~0x20) >= 0x41 && ch <= 0x5a)
  199.         )
  200.         p++, sp++;
  201.     else if (pback == 0)
  202.         return false;    /* no * to back up to */
  203.     else {
  204.         sp = ++spback;
  205.         p = pback;
  206.     }
  207.     }
  208.     if (sp < spend) {        /* We got a match, but there are chars left over. */
  209.     /* If we can back up, back up to the only place that */
  210.     /* could produce a complete match, otherwise fail. */
  211.     if (pback == 0)
  212.         return false;
  213.     p = pback;
  214.     pback = 0;
  215.     sp = spend - (pend - p);
  216.     goto again;
  217.     }
  218.     return true;
  219. }
  220.  
  221. /* ------ UID utilities ------ */
  222.  
  223. /* Compare two UIDs for equality. */
  224. /* We know that at least one of them is valid. */
  225. bool
  226. uid_equal(register const gs_uid * puid1, register const gs_uid * puid2)
  227. {
  228.     if (puid1->id != puid2->id)
  229.     return false;
  230.     if (puid1->id >= 0)
  231.     return true;        /* UniqueID */
  232.     return
  233.     !memcmp((const char *)puid1->xvalues,
  234.         (const char *)puid2->xvalues,
  235.         (uint) - (puid1->id) * sizeof(long));
  236. }
  237.  
  238. /* ------ Rectangle utilities ------ */
  239.  
  240. /*
  241.  * Calculate the difference of two rectangles, a list of up to 4 rectangles.
  242.  * Return the number of rectangles in the list, and set the first rectangle
  243.  * to the intersection.
  244.  */
  245. int
  246. int_rect_difference(gs_int_rect * outer, const gs_int_rect * inner,
  247.             gs_int_rect * diffs /*[4] */ )
  248. {
  249.     int x0 = outer->p.x, y0 = outer->p.y;
  250.     int x1 = outer->q.x, y1 = outer->q.y;
  251.     int count = 0;
  252.  
  253.     if (y0 < inner->p.y) {
  254.     diffs[0].p.x = x0, diffs[0].p.y = y0;
  255.     diffs[0].q.x = x1, diffs[0].q.y = min(y1, inner->p.y);
  256.     outer->p.y = y0 = diffs[0].q.y;
  257.     ++count;
  258.     }
  259.     if (y1 > inner->q.y) {
  260.     diffs[count].p.x = x0, diffs[count].p.y = max(y0, inner->q.y);
  261.     diffs[count].q.x = x1, diffs[count].q.y = y1;
  262.     outer->q.y = y1 = diffs[count].p.y;
  263.     ++count;
  264.     }
  265.     if (x0 < inner->p.x) {
  266.     diffs[0].p.x = x0, diffs[0].p.y = y0;
  267.     diffs[0].q.x = min(x1, inner->p.x), diffs[0].q.y = y1;
  268.     outer->p.x = x0 = diffs[count].q.x;
  269.     ++count;
  270.     }
  271.     if (x1 > inner->q.x) {
  272.     diffs[count].p.x = max(x0, inner->q.x), diffs[count].p.y = y0;
  273.     diffs[count].q.x = x1, diffs[count].q.y = y1;
  274.     outer->q.x = x1 = diffs[count].p.x;
  275.     ++count;
  276.     }
  277.     return count;
  278. }
  279.